package com.wongoing.aop;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class FrontInterceptor  extends HandlerInterceptorAdapter{
	/**
	 * 开始处理请求
	 */
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		System.out.println("-----拦截开始：");
		request.setCharacterEncoding("utf-8");
		
		System.out.println("user:"+request.getUserPrincipal());
		System.out.println("url:"+request.getRequestURL());
		System.out.println("addr:"+request.getRemoteAddr());
		System.out.println("host:"+request.getRemoteHost());
		System.out.println("port:"+request.getRemotePort());
		System.out.println("method:"+request.getMethod());
		Enumeration em = request.getParameterNames();
		 while (em.hasMoreElements()) {
		    String name = (String) em.nextElement();
		    String value = request.getParameter(name);
		    System.out.println("name:"+name+" | value:"+value);
		}
		System.out.println("url=" + request.getRequestURI());
		System.out.println("&&&:"+arg2.getClass().getCanonicalName());
		System.out.println(arg2.getClass().getSimpleName());
		return true;
	}
	/**
	 * 请求处理完毕，页面呈现之前
	 */
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3) throws Exception {
		System.out.println("-----调用controller后进入");
		System.out.println("url=" + request.getRequestURI());

	}
	
	/**
	 * 页面呈现后指定的回调
	 */
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3)
			throws Exception {
		System.out.println("------回调执行");
		System.out.println("url=" + request.getRequestURI());
	}

}
